//
//  XFComposePhotosView.m
//  Weibo
//
//  Created by Fay on 15/10/16.
//  Copyright (c) 2015年 Fay. All rights reserved.
//

#import "XFComposePhotosView.h"
#import "CHTCollectionViewWaterfallLayout.h"
#import "MHUploadImage.h"

@interface XFComposePhotosView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@end

@implementation XFComposePhotosView

-(id)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        _photos = [NSMutableArray array];
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        // 设置UICollectionView为横向滚动
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        // 每一行cell之间的间距
        flowLayout.minimumLineSpacing = 10;
        // 每一列cell之间的间距
//         flowLayout.minimumInteritemSpacing = 10;
        // 设置第一个cell和最后一个cell,与父控件之间的间距
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _imageCollection = [[MHCollectionView alloc] initWithFrame:CGRectMake(0, 0, MH_SCREEN_WIDTH, 80) collectionViewLayout:flowLayout];
        _imageCollection.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    
        [_imageCollection setContentSize:CGSizeMake(MH_SCREEN_WIDTH, 0)];
        _imageCollection.dataSource = self;
        _imageCollection.delegate = self;
        _imageCollection.backgroundColor = [UIColor clearColor];
        _imageCollection.userInteractionEnabled = YES;
        [_imageCollection registerClass:[UICollectionViewCell class]
            forCellWithReuseIdentifier:@"UICollectionCell"];
        [self addSubview:_imageCollection];
        [_imageCollection mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
    }
    return self;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.selectImageBlock) {
        self.selectImageBlock(indexPath.item);
    }
}

-(void)addPhoto:(MHUploadImage *)photo {
    [self.photos addObject:photo]; 
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionCell" forIndexPath:indexPath];
    MHUploadImage *image =  self.photos[indexPath.item];
    UIImage *uploadImage;
    if (image.isGif) {
        uploadImage = [YYImage imageWithData:image.orginalData];
    }else
    {
        uploadImage =  image.image;
    }
    YYAnimatedImageView *animatedImage = [[YYAnimatedImageView alloc] initWithImage:uploadImage];
    animatedImage.frame = cell.contentView.frame;
    animatedImage.contentMode = UIViewContentModeScaleAspectFill;
    animatedImage.clipsToBounds = YES;
    [cell.contentView addSubview:animatedImage];

    if (image.isVideoCover) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:MHImageNamed(@"icon_play_w") forState:0];
        button.frame = animatedImage.frame;
        [animatedImage addSubview:button];
    }
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.photos.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
     return  CGSizeMake(80, 80);
}


-(void)layoutSubviews {
    
    [super layoutSubviews];
    if (self.photos.count) {
        self.height = 80;
    }else
    {
        self.height = 0;
    }
}


@end

